package com.doupi.partner.op.controller.business;

import com.doupi.partner.constant.PartnerTypeEnum;
import com.doupi.partner.manager.BusinessRelationManager;
import com.doupi.partner.model.BusinessRelation;
import com.doupi.partner.model.PartnerRelation;
import com.doupi.partner.op.util.OpUtils;
import com.doupi.partner.service.BusinessRelationService;
import com.doupi.partner.service.dto.AdministratorDto;
import com.doupi.partner.util.*;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 商家
 *
 *
 * @author Alvin Wang
 * @version 1.0.0 @ 2017-04-16
 */
@Controller
@RequestMapping("business/{type}")
public class BusinessController {
	private final static String TEMPLATE_FOLDER = "business/";

	private final BusinessRelationService businessRelationService;

	private final BusinessRelationManager businessRelationManager;

	@Autowired
	public BusinessController(BusinessRelationService businessRelationService, BusinessRelationManager businessRelationManager) {
		this.businessRelationService = businessRelationService;
		this.businessRelationManager = businessRelationManager;
	}

	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String index(ModelMap modelMap, @PathVariable("type") Integer type) {
		modelMap.put("type", type);
		return TEMPLATE_FOLDER + "index";
	}

	@ResponseBody
	@RequestMapping(value = "list", method = RequestMethod.POST)
	public DataTableData<BusinessRelation> list(@PathVariable("type") Byte type, Integer offset, Integer limit, BusinessRelation businessRelation) throws Exception {
		AdministratorDto administratorDto = OpUtils.getCurrentLoginAdministrator();
		PartnerRelation administratorDtoPartnerRelation = administratorDto.getPartnerRelation();
		PartnerRelation administratorRelation = administratorDto.getPartnerRelation();
		PartnerTypeEnum partnerType = PartnerTypeEnum.getPartnerTypeByCode(administratorRelation.getType());
		AssertUtils.assertNotNull(partnerType, "合伙人类型正确");
		if (partnerType != PartnerTypeEnum.HEADQUARTERS) {
			switch (type) {
			case 1: // 直属商家
				businessRelation.setPartnerAdministratorId(administratorDtoPartnerRelation.getAdministratorId());
				break;
			case 2: // 二级商家
				businessRelation.setPartnerAdministratorId(-administratorDtoPartnerRelation.getAdministratorId()); // 不是自己添加的
				businessRelation.setSuperiorPartnerAdministratorId(administratorDtoPartnerRelation.getAdministratorId());
				businessRelation.setCityAdministratorId(administratorDtoPartnerRelation.getAdministratorId());
				businessRelation.setSubstationAdministratorId(administratorDtoPartnerRelation.getAdministratorId());
				break;
			default:
				throw RuntimeBusinessException.createException("未知的商家类型");
			}
		}

		Pagination<BusinessRelation, BusinessRelation> pagination = new Pagination<>(offset, limit, businessRelation);
		pagination = businessRelationService.queryBusinessRelationsWithAnnotation(pagination);
		List<BusinessRelation> lists = businessRelationManager.convertBusinessRelationListToDtoList(pagination.getRecords());
		return new DataTableData<>(pagination.getTotalCount(), lists);
	}

	// @ResponseBody
	// @RequestMapping(value = "delete/{id}", method = RequestMethod.DELETE)
	// public AjaxResponse delete(@PathVariable("id") Long id) {
	// businessRelationService.deleteByPrimaryKey(id);
	// return AjaxResponse.defaultResponse();
	// }

	@RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
	public String edit(ModelMap modelMap, @PathVariable("type") Byte type, @PathVariable("id") Long id) {
		BusinessRelation businessRelation = businessRelationService.selectByPrimaryKey(id);
		if (null != businessRelation) {
			modelMap.put("businessRelation", businessRelationManager.convertBusinessRelationToDto(businessRelation));
		}

		modelMap.put("type", type);
		return TEMPLATE_FOLDER + "edit";
	}

	@RequestMapping(value = "save", method = RequestMethod.POST)
	public String save(String mobile, String name, BusinessRelation businessRelation, String password) {
		AdministratorDto administratorDto = OpUtils.getCurrentLoginAdministrator();

		if (null == businessRelation.getId()) {
			PartnerRelation administratorDtoPartnerRelation = administratorDto.getPartnerRelation();
			PartnerTypeEnum partnerType = PartnerTypeEnum.getPartnerTypeByCode(administratorDtoPartnerRelation.getType());
			AssertUtils.assertNotNull(partnerType, "异常操作");
			switch (partnerType) {
			case HEADQUARTERS: // 总账号添加
				businessRelation.setPartnerAdministratorId(administratorDtoPartnerRelation.getAdministratorId());
				businessRelation.setSuperiorPartnerAdministratorId(administratorDtoPartnerRelation.getAdministratorId());
				businessRelation.setCityAdministratorId(administratorDtoPartnerRelation.getAdministratorId());
				businessRelation.setSubstationAdministratorId(administratorDtoPartnerRelation.getAdministratorId());
				break;
			case SUBSTATION: // 分站账号添加
				businessRelation.setPartnerAdministratorId(administratorDtoPartnerRelation.getAdministratorId());
				businessRelation.setSuperiorPartnerAdministratorId(administratorDtoPartnerRelation.getSuperiorAdministratorId());
				businessRelation.setCityAdministratorId(administratorDtoPartnerRelation.getCityAdministratorId());
				businessRelation.setSubstationAdministratorId(administratorDtoPartnerRelation.getSubstationAdministratorId());
				break;
			case CITY: // 城市代理商添加
				businessRelation.setPartnerAdministratorId(administratorDtoPartnerRelation.getAdministratorId());
				businessRelation.setSuperiorPartnerAdministratorId(administratorDtoPartnerRelation.getSuperiorAdministratorId());
				businessRelation.setCityAdministratorId(administratorDtoPartnerRelation.getAdministratorId());
				businessRelation.setSubstationAdministratorId(administratorDtoPartnerRelation.getSubstationAdministratorId());
				break;
			case SUPERIOR: // 一级合伙人添加
				businessRelation.setPartnerAdministratorId(administratorDtoPartnerRelation.getAdministratorId());
				businessRelation.setSuperiorPartnerAdministratorId(administratorDtoPartnerRelation.getSuperiorAdministratorId());
				businessRelation.setCityAdministratorId(administratorDtoPartnerRelation.getCityAdministratorId());
				businessRelation.setSubstationAdministratorId(administratorDtoPartnerRelation.getSubstationAdministratorId());
				break;
			case SECOND: // 二级合伙人添加
				businessRelation.setPartnerAdministratorId(administratorDtoPartnerRelation.getAdministratorId());
				businessRelation.setSuperiorPartnerAdministratorId(administratorDtoPartnerRelation.getSuperiorAdministratorId());
				businessRelation.setCityAdministratorId(administratorDtoPartnerRelation.getCityAdministratorId());
				businessRelation.setSubstationAdministratorId(administratorDtoPartnerRelation.getSubstationAdministratorId());
				break;
			default:
				break;
			}

			if (StringUtils.isBlank(businessRelation.getCity())) {
				businessRelation.setCity(administratorDtoPartnerRelation.getCity());
			}
			// TODO 提取
			businessRelation.setMarketingNumber("1" + RandomStringUtils.random(7, "1234567890"));
		}

		businessRelationManager.saveBusinessRelation(mobile, name, businessRelation, password);
		return "redirect:index";
	}

}
